package cn.com.idmy.cloud.controller;

import com.alibaba.druid.pool.DruidDataSource;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "Druid监控")
@RequestMapping("/api/monitor")
public class DruidMonitorController {
    @Autowired
    private DataSource dataSource;

    @RequestMapping("/connections")
    public Map<String, Object> connections() {
        var result = new HashMap<String, Object>();
        if (dataSource instanceof DruidDataSource ds) {
            result.put("maxActive", ds.getMaxActive());
            result.put("activeCount", ds.getActiveCount());
            result.put("poolingCount", ds.getPoolingCount());
            result.put("waitThreadCount", ds.getWaitThreadCount());
            result.put("activePeak", ds.getActivePeak());
        }
        return result;
    }
}